package Retail;


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RetailProductSelect extends HttpServlet {
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req,resp);
	}

	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req,resp);
	}
	protected void doProcess(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<RetailProduct> item = new ArrayList<RetailProduct>();
		RetailProduct product = new RetailProduct();
		String InputText= req.getParameter("PID");
		if (InputText != null){	
		try {
			Class.forName("org.h2.Driver");
			Connection con = DriverManager.getConnection("jdbc:h2:~/retail",
					"sa", "");
			PreparedStatement ps = con.prepareStatement("SELECT * FROM PRODUCT Where PRODUCTID=?");
		    ps.setString(1, InputText);
			ResultSet rs = ps.executeQuery();
			
		//	System.out.print(rs.next());
			//System.out.print(rs);
			while (rs.next()) {
				
				product.setRProductID(rs.getString("PRODUCTID"));
				product.setRProductDesc(rs.getString("DESCRIPTION"));
				product.setRProductPrice(rs.getBigDecimal("SOLD_PRICE"));
				product.setRProductCost(rs.getBigDecimal("COST_PRICE"));
				product.setRProductQuantity(rs.getInt("QUANTITY"));
				product.setRProductVaildDate(rs.getInt("VALID_DATE"));
				product.setRProductEntryDate(rs.getDate("WAREHOUSING_TIME"));
				item.add(product);
		} 
			
			ps.close();
			con.close();
			
			
			
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}
		req.setAttribute("a", item);
	req.getRequestDispatcher("Product/ProductSelect.jsp").forward(req, resp);	
	}

}
